Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControls
Imports DotNetNuke.UI.Utilities

Namespace DotNetNuke.UI.WebControls
	<ProvideProperty("Toolbar", GetType(System.Web.UI.Control)), ParseChildren(True, "Toolbars"), DefaultProperty("Toolbars"), PersistChildren(False), Designer(GetType(Design.WebControls.DNNToolBarComponentDesigner))> _
	Public Class DNNToolBarComponent
		Inherits Control
		Implements IExtenderProvider

#Region "Member Variables"
		Private m_objToolbars As DNNToolBarCollection
		Private m_objControl As Control
#End Region

#Region "Extender Implementation"
		'for more info see http://www.codeproject.com/aspnet/PropertyExtendersASP20.asp

		<DesignerSerializationVisibility(DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerDefaultProperty), Browsable(False)> _
		Public Overridable ReadOnly Property Toolbars() As DNNToolBarCollection
			Get
				If m_objToolbars Is Nothing Then
					m_objToolbars = New DNNToolBarCollection(Me)
				End If
				Return CType(m_objToolbars, DNNToolBarCollection)
			End Get
		End Property

		Public Function CanExtend(ByVal extendee As Object) As Boolean Implements System.ComponentModel.IExtenderProvider.CanExtend
			Select Case True
				Case TypeOf extendee Is DNNLabelEdit
					Return True
			End Select
			Return False
		End Function

		Public Sub SetToolbar()
			Dim x As Boolean = False
		End Sub

		Public Function GetToolbar(ByVal objControl As Control) As DNNToolBar
			m_objControl = objControl
			Dim objToolbar As DNNToolBar = Toolbars.FindToolBar(objControl.ID)
			If Not objToolbar Is Nothing Then
				Return objToolbar
			End If
			objToolbar = New DNNToolBar
			objToolbar.Target = objControl.ID

			'objToolbar.HostingControl = Me
			m_objToolbars.Add(objToolbar)
			Me.NotifyDesigner()
			Return objToolbar
		End Function


#End Region

		Friend Sub NotifyDesigner()
			If IsDesignMode() Then
				Dim dh As IDesignerHost = DirectCast(MyBase.Site.Container, IDesignerHost)
				Dim designer As ControlDesigner = CType(dh.GetDesigner(Me), ControlDesigner)
				Dim pd As PropertyDescriptor = TypeDescriptor.GetProperties(Me).Item("Toolbars")
				Dim objArgs As ComponentChangedEventArgs = New ComponentChangedEventArgs(Me, pd, Nothing, Me.Toolbars)
				designer.OnComponentChanged(Me, objArgs)
			End If
		End Sub

		Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

			For Each objToolbar As DNNToolBar In Me.Toolbars
				ClientAPI.RegisterStartUpScript(Page, objToolbar.ClientID & "_startup", "<script>" & GetToolbarScript(objToolbar.ToJSON(), objToolbar.Target) & ";</script>")
			Next
		End Sub

		Private Function GetToolbarScript(ByVal strJSON As String, ByVal strTarget As String) As String
			If Not HttpContext.Current Is Nothing Then
				Dim strCacheKey As String = "__DNNToolBarComponent"
				Dim objCache As System.Web.Caching.Cache = HttpContext.Current.Cache
				Dim objHash As Hashtable = CType(objCache(strCacheKey), Hashtable)
				If objHash Is Nothing Then
					objHash = New Hashtable
					objCache("__DNNToolBarComponent") = objHash
				End If
				If objHash.ContainsKey(strJSON) Then
					strJSON = String.Format("dnn.controls.toolbars['{0}']", objHash(strJSON))
				Else
					objHash.Add(strJSON, strTarget)
				End If
			End If
			Return String.Format("dnn.controls.toolbars['{0}']={1}", strTarget, strJSON)
		End Function

	End Class
End Namespace